﻿using CBS.APP;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// EditStudent.xaml 的交互逻辑
    /// </summary>
    public partial class EditStudent : Window
    {
        private StudentViewModel viewModel;

        public EditStudent()
        {
            InitializeComponent();
            viewModel = new StudentViewModel();
        }

        public EditStudent(int StudentId)
        {
            InitializeComponent();

            this.viewModel = StudentApp.GetForm(StudentId);
            tb_StudentName.Text = viewModel.StudentName;
            tb_PhoneNumber.Text = viewModel.PhoneNumber;
            tb_WxCode.Text = viewModel.WxCode;
            tb_Account.Text = viewModel.Balance.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tb_StudentName.Text)) 
            {
                MessageBox.Show(this,"姓名不能为空。","警告",MessageBoxButton.OK,MessageBoxImage.Warning);
                return;
            }

            viewModel.StudentName = tb_StudentName.Text;
            viewModel.PhoneNumber = tb_PhoneNumber.Text;
            viewModel.WxCode = tb_WxCode.Text;
            bool isCorrect = decimal.TryParse(tb_Account.Text, out decimal balance);
            viewModel.Balance = balance;

            if (StudentApp.UpdateStudentInfo(viewModel)) 
            {
                this.DialogResult = true;
            }

        }
    }
}
